package lu.med.praxiswahl.wahlumed.dao.beans;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.FormularDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.CustomerTBL;
import lu.med.praxiswahl.wahlumed.model.beans.FormularTBL;

/**
 * Session Bean implementation class FormularDAO
 */
@Stateless
@Local({FormularDAOInt.class})
public class FormularDAO implements FormularDAOInt {

	@PersistenceContext(unitName = "gynaekomedic-ejbPU")
    private EntityManager em = null;
	
    /**
     * Default constructor. 
     */
    public FormularDAO() {
    }

	@Override
	public void delete(Long todo)
	{
		FormularTBL item = em.find(FormularTBL.class, todo);

        CustomerTBL cust = item.getCustomer();

        cust.getFormularEintraege().remove(item);

        em.remove(item);
        em.flush();
		
	}

	@Override
	public void persist(FormularTBL todo)
	{
		em.persist(todo);
        em.flush();
		
	}

	@Override
	public FormularTBL retrieveFormularItemByID(Long id)
	{
		return em.find(FormularTBL.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FormularTBL> retrieveFormularItemsByCustomer(
			String matricule)
	{
		 try {
	            return (List<FormularTBL>) em.createQuery(
	                    "select f from FormularTBL f where f.customer = '" + matricule + "' order by f.datum desc").getResultList();
	        } catch (Exception e) {
	            throw new RuntimeException(
	                    "Could not retrieve Formular item for this Customer", e);
	        }
	}

	@Override
	public void update(FormularTBL todo)
	{
		em.merge(todo);
        em.flush();
		
	}

	@Override
	public Long findSubPrimaryKeyIndexBYTyp(int typ)
	{
		try
		{
			
			Long result = (Long)em
					.createQuery(
							"select count(typ) from FormularTBL f where f.typ = "+typ+" order by f.datum desc").getSingleResult();
			
			
			return result.longValue();
		}
		catch (Exception e)
		{
			/*throw new RuntimeException("Houston we have a problem",
					e);*/
			return 0l;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FormularTBL> retrieveFormularBY_Typ(String typ)
	{
		try {
            return (List<FormularTBL>) em.createQuery(
                    "select f from FormularTBL f where f.typ = '" + typ + "' order by f.datum desc").getResultList();
        } catch (Exception e) {
            throw new RuntimeException(
                    "Could not retrieve Formular item for this type", e);
        }
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FormularTBL> retrieveFormular_BYCustomer_and_BYtyp(
			String matricule, String typ)
	{
		try {
            return (List<FormularTBL>) em.createQuery(
                    "select f from FormularTBL f where f.customer = '" + matricule + "' and f.typ = '"+typ+"'" +
                    		"order by f.datum desc").getResultList();
        } catch (Exception e) {
            throw new RuntimeException(
                    "Could not retrieve the needed Formulars", e);
        }
	}

}
